function gmm=calc_gmm3(params, A_t,  M_t, BI_t, Nbar_i, HH_bin, CollPerc_t, Pol_t, Own_t, Inc_t, ...
        Sunit_t, Scost_t, Skwh_t, P_t, PSale_t, PIns_t, PIns0_t)


global just_size model_type

global distance_border tract_border_xxwalk_mat population

global PropTax IncTax SalesTax


[V_inst, N_i, sub_bin]=calc_V_inst2(params, A_t, Nbar_i,  CollPerc_t, Pol_t, Own_t, Inc_t, ...
    Sunit_t, Scost_t, Skwh_t, P_t, PSale_t, PIns_t, PIns0_t);

Prob_sim_bin=(exp(V_inst))./(1+exp(V_inst));

Prob_sim_bin(V_inst>10)=1;

Prob_sim_b_t=sum(Prob_sim_bin.*HH_bin,2)./sum(HH_bin,2);
%numerator is buildings with installs. Denominator is total houses

Prob_data_b=BI_t./sum(HH_bin,2);

Prob_data_b(Prob_data_b>1)=1;

weight=calc_weight(sum(HH_bin,2),BI_t);

gmm_tract=(log(Prob_data_b)-log(Prob_sim_b_t)   ).*weight.*(log(Prob_data_b)-log(Prob_sim_b_t)   );

gmm_tract(Prob_data_b==0)=0;
gmm_tract(sum(HH_bin,2)==0)=0;



gmm=sum(gmm_tract);





%% variation in size

size_data_t=M_t./BI_t;

size_data_t(BI_t==0)=0;

size_sim_t=sum(Prob_sim_bin.*HH_bin.*N_i, 2) ./ sum( HH_bin.*Prob_sim_bin, 2);

size_sim_t(BI_t==0)=0;


weight=calc_weight(sum(HH_bin,2),BI_t)./nanmean(size_data_t);

gmm_tract_size=(size_data_t-size_sim_t   ).*weight.*(size_data_t-size_sim_t   );

size1=gmm;
size2=sum(gmm_tract_size);

gmm=gmm+sum(gmm_tract_size);


%% border discontintuity moment

%if strcmp(model_type,'NoDemD')==1 || strcmp(model_type,'CollPercD')==1 || strcmp(model_type,'CollPercPolD')==1 || strcmp(model_type,'CollPercPolOwnD')==1

%global distance_border tract_border_xxwalk
    sub_amt_base_t=calc_sub(15*ones(size(Nbar_i)), A_t,  ...
    Sunit_t, Scost_t, Skwh_t, P_t, PSale_t, PIns_t, PIns0_t);  

    exp_sub_t=sub_amt_base_t(:,1); % just one per tract and all bins are identical here

    mon_ben=15.*A_t.*(P_t)-(PIns0_t+ PIns_t.*15)+exp_sub_t;

    include=(distance_border<10) & (Prob_data_b>0);

    HH_t=sum(HH_bin,2);

    BI_sim=Prob_sim_b_t.*sum(HH_bin,2);

    M_bin=HH_bin.*N_i.*Prob_sim_bin;

    M_sim=sum(M_bin,2);

        [reg_sim, SE1]=lscov([mon_ben(include) ...
           Pol_t(include) CollPerc_t(include)  PropTax(include) IncTax(include) SalesTax(include) tract_border_xxwalk_mat(include,1:end-1)],...
            log(M_sim(include)), HH_t(include));



        [reg_data, SEData]=lscov([mon_ben(include)  ...
            Pol_t(include) CollPerc_t(include)  PropTax(include) IncTax(include) SalesTax(include) tract_border_xxwalk_mat(include,1:end-1)],...
            log(M_t(include)) , population(include));


        
gmm_border=(reg_data(1,1)-reg_sim(1,1)).*(80000000./SEData(1,1)).*(reg_data(1,1)-reg_sim(1,1));



reg_data(1,1);
reg_sim(1,1);

gmm=gmm+gmm_border;

%end
